import { _decorator, Component, director, Label, Node, tween, v2, v3 } from 'cc';
import { AdComponent } from '../AdComponent';
import DatasSpace from '../Tool/DatasSpace';
import { Home } from '../Home';
import Util from '../Util';
import { AudioMgr, ENUM_CLIP } from '../Tool/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('LuckBox')
export class LuckBox extends Component {

  @property(Node)
  viewNode: Node = null;

  @property(Label)
  countTimeLbl: Label = null;

  @property(Node)
  videoBtnG: Node = null;

  @property(Node)
  videoBtnP: Node = null;

  btnClose() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    this.btnCloseSetting();
  }

  start() {
    // AdComponent.Instance.show5X5CustomAd();
    AdComponent.Instance.showInterstitialFullShow();
    AdComponent.Instance.showBannerAd();
    // tween(this.viewNode).to(0.3, { position:v3(this.viewNode.getPosition().x,-50,0), scale:v3(1.2,0.8,0)}, { easing: 'smooth' }).to(0.3, { position: v3(0,0,0), scale:v3(1,1,0) }, { easing: 'backInOut' }).to(0.3, { scale:v3(1.1,0.9,0)}, { easing: 'backInOut' }).to(0.1, { scale:v3(1,1,0) }, { easing: 'backInOut' }).call(() => {
    // }).start();
    this.updTimeLbl();
    this.setBtnAni();
  }

  protected onDestroy(): void {
    AdComponent.Instance.hideBannerAd();
  }

  btnCloseSetting() {
    // AdComponent.Instance.instance.playAudio(2);
    // AdComponent.Instance.hide5X5customAd();
    tween(this.viewNode).to(0.5, { position: v3(0, this.viewNode.getPosition().y + 1600, 0) }, { easing: 'backInOut' }).call(() => {
      this.node.removeFromParent();
      this.node.destroy();

    }).start();
  }

  btnVideoGetLuckyBoxReward() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);
    AdComponent.Instance.loadVideoAd(0, (res) => {
      if (res) {
        console.log('广告购买成功');
        DatasSpace.Instance.goldNum += 100;
        DatasSpace.Instance.setGoldNum();
        //增加翻牌道具
        DatasSpace.Instance.propNumArr[1] += 1;
        //增加消除道具
        DatasSpace.Instance.propNumArr[2] += 1;
        //增加加时道具
        DatasSpace.Instance.propNumArr[4] += 1;
        //增加洗牌道具
        DatasSpace.Instance.propNumArr[0] += 1;
        DatasSpace.Instance.setPropNum();
        this.btnCloseSetting();
        Home.inst.checkUserGoldNumAndFtNum();
        Home.inst.popReward(2);

        const Sec = 20 * 60;
        DatasSpace.Instance.setInt('luckyBoxReward', Sec)
        DatasSpace.Instance.opeanLuckyBoxRewardCountDown();
      }
    });
  }

  updTimeLbl() {
    let sec = DatasSpace.Instance.getLuckyBoxRewardSec();
    let timeInfo = Util.Instance.getTimeByScend(sec);
    this.countTimeLbl.string = timeInfo.hour + ':' + timeInfo.min + ':' + timeInfo.scend;
    if (sec > 0) {
      this.videoBtnP.active = true;
      this.videoBtnG.active = false;
    } else {
      this.videoBtnP.active = false;
      this.videoBtnG.active = true;
    }
  }

  private updSec: number = 0;
  update(deltaTime: number) {
    this.updSec += deltaTime;
    if (this.updSec >= 1) {
      this.updTimeLbl();
      this.updSec = 0;
    }
  }

  setBtnAni() {
    let icon = this.videoBtnP.getChildByName('icon');
    tween(icon)
      .to(0.25, { angle: 10 })
      .to(0.25, { angle: -10 })
      .to(0.25, { angle: 10 })
      .to(0.25, { angle: -10 })
      .delay(0.5)
      .union()
      .repeatForever()
      .start();

  }
}


